# Author: HZT
# Date: 2023/9/6
import allure
import pyautogui
import page
from config.config_loader import config
from page.web_ck_top import WebCkTop
from page.web_sk_jk import WebSkJk
from page.web_sk_top import WebSkTop
from utils import UtilsDriver, click_paste_img, generate_random_string, generate_random_string_sz, \
    login_with_session, GetLog, UtilsDriver2
from page.web_ck_dd import WebCkDd
import time
from selenium import webdriver


class TestWebSkJk:
    def setup_class(self):
        self.log = GetLog.get_log()
        self.driver = UtilsDriver.get_web_driver()
        self.driver.get(config.URL)
        login_with_session('system2', self.driver)
        self.driver2 = UtilsDriver2.get_web_driver()
        self.driver2.get(config.URL)
        login_with_session('system1', self.driver2)
        pyautogui.hotkey('alt', 'tab')
        # 实例化
        self.skJk = WebSkJk(self.driver)
        self.ckdd = WebCkDd(self.driver2)
        self.skTop = WebSkTop(self.driver)
        self.ckTop = WebCkTop(self.driver2)
        self.skJk.sk_jk_click_jk()
        # 定位开关元素
        switch = self.skJk.sk_jk_get_ksjk()
        # 判断开关状态
        if switch.get_attribute("aria-hidden") == "true":
            # 开关是关闭状态，点击开关
            self.skJk.sk_jk_click_ksjk()

    def teardown_class(self):
        # 在所有测试用例执行完毕后，关闭浏览器
        # UtilsDriver.get_web_driver().quit()
        pass

    # 处理成功(发两张卡t-Steam，ocr识别正确)
    # 1.BLOCKER最严重
    # 2.CRITICAL严重
    # 3.NORMAL普通
    # 4.MINOR不严重
    # 5.TRIVIAL最不严重
    @allure.severity(allure.severity_level.MINOR)
    def test01_sk_jk(self):
        # self.driver.execute_script("window.open('', '_blank');")
        # 切换到新窗口
        # self.skJk.base_switch_window(1)
        pyautogui.hotkey('alt', 'tab')
        count = 2  # 发卡数
        me = '100'  # 发卡金额
        filename = "steam_card.png"  # 卡图名称
        self.ckdd.ck_dd_click_jdd()
        self.ckdd.ck_dd_ck(self.driver2, me, filename, count)
        try:
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            self.skTop.sk_top_click_sx()
            # 获取交易前余额
            before_ye = self.skTop.sk_top_get_ye().replace(',', '')
            self.skJk.sk_jk_scyet(self.driver)
            for i in range(count):
                dm = self.skJk.sk_jk_get_dms()[i].text.split('：')[1]
                assert '5FCH3WWNJBVGVW7' == dm
            self.skJk.sk_jk_click_clcg()
            self.skJk.sk_jk_click_cgtj()
            self.skTop.sk_top_click_sx()
            time.sleep(1)
            # 获取交易后余额
            after_ye = self.skTop.sk_top_get_ye().replace(',', '')
            yec = float(before_ye) - float(after_ye)
            assert "880.0" == str(yec)
        except Exception as e:
            self.log.error("接卡异常,原因{}".format(e))
            # 截图
            allure.attach(self.driver.get_screenshot_as_png(), "截图", allure.attachment_type.PNG)
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 处理成功(发t-Sephora，ocr识别正确)
    def test02_sk_jk(self):
        pyautogui.hotkey('alt', 'tab')
        time.sleep(2)
        count = 1  # 发卡数
        me = '100'  # 发卡金额
        self.ckdd.ck_dd_click_jdd()
        self.ckdd.ck_dd_ck(self.driver2, me, "sephora_card1.png", count, "t-Sephora")
        try:
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            self.skTop.sk_top_click_sx()
            # 获取交易前余额
            before_ye = self.skTop.sk_top_get_ye().replace(',', '')
            self.skJk.sk_jk_click_ckkm()
            dm = self.skJk.sk_jk_get_dm()
            pin = self.skJk.sk_jk_get_pin()
            assert '6203496513632674' == dm.split('：')[1]
            assert '47255481' == pin.split('：')[1]
            self.skJk.sk_jk_click_clcg()
            self.skJk.sk_jk_click_cgtj()
            self.skTop.sk_top_click_sx()
            time.sleep(1)
            # 获取交易后余额
            after_ye = self.skTop.sk_top_get_ye().replace(',', '')
            yec = float(before_ye) - float(after_ye)
            assert "880.0" == str(yec)
        except Exception as e:
            self.log.error("错误原因{}".format(e))
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 处理成功(t-Sephora卡，实体卡，带代码，PIN,填写16位卡密和8位PIN)
    def test03_sk_jk(self):
        pyautogui.hotkey('alt', 'tab')
        time.sleep(2)
        count = 1  # 发卡数
        me = '100'  # 发卡金额
        self.ckdd.ck_dd_click_jdd()
        km = generate_random_string_sz(16)
        pin = generate_random_string_sz(8)
        self.ckdd.ck_dd_ck(self.driver2, me, "sephora_card1.png", count, "t-Sephora", km, pin)
        try:
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            self.skTop.sk_top_click_sx()
            # 获取交易前余额
            before_ye = self.skTop.sk_top_get_ye().replace(',', '')
            self.skJk.sk_jk_click_ckkm()
            km1 = self.skJk.sk_jk_get_dm()
            pin1 = self.skJk.sk_jk_get_pin()
            assert km == km1.split('：')[1]
            assert pin == pin1.split('：')[1]
            self.skJk.sk_jk_click_clcg()
            self.skJk.sk_jk_click_cgtj()
            self.skTop.sk_top_click_sx()
            time.sleep(1)
            # 获取交易后余额
            after_ye = self.skTop.sk_top_get_ye().replace(',', '')
            yec = float(before_ye) - float(after_ye)
            assert "880.0" == str(yec)
        except Exception as e:
            self.log.error("错误原因{}".format(e))
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 处理失败失败（失败原因：卡片信息有误）
    def test04_sk_jk(self):
        pyautogui.hotkey('alt', 'tab')
        time.sleep(2)
        count = 1  # 发卡数
        me = '100'  # 发卡金额
        filename = "steam_card.png"  # 卡图名称
        self.ckdd.ck_dd_click_jdd()
        km = generate_random_string(15)
        self.ckdd.ck_dd_ck(self.driver2, me, filename, count, km=km)
        try:
            pyautogui.hotkey('alt', 'tab')
            self.skTop.sk_top_click_sx()
            self.skJk.sk_jk_scyet(self.driver)
            # 获取页面卡密
            ymkm = self.skJk.sk_jk_get_dm().split('：')[1]
            self.skJk.sk_jk_click_clsb()
            time.sleep(1)
            self.skJk.sk_jk_click_sbyy()
            time.sleep(1)
            self.skJk.sk_jk_click_xxyw()
            time.sleep(1)
            # 点击失败提交
            self.skJk.sk_jk_click_sbtj()
            time.sleep(1)
            text = self.skJk.sk_jk_get_tpts()
            assert ymkm == km
            assert "上传失败图片" in text
        except Exception as e:
            self.log.error(e)
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise

    # 处理失败成功（失败原因：卡片信息有误）
    def test05_sk_jk(self):
        click_paste_img("test001.png", self.skJk.sk_jk_get_sbtp(), self.driver)
        time.sleep(1)
        # 点击提交
        self.skJk.sk_jk_click_sbtj()
        time.sleep(3)
        text = self.skJk.sk_jk_get_tcts()
        time.sleep(1)
        assert "成功" in text
        self.skJk.sk_jk_click_tcok()
        time.sleep(5)

    # 修改卡种失败（修改不存在汇率的金额）
    def test06_sk_jk(self):
        pyautogui.hotkey('alt', 'tab')
        time.sleep(2)
        count = 1  # 发卡数
        me = '100'  # 发卡金额
        filename = "steam_card.png"  # 卡图名称
        self.ckdd.ck_dd_click_jdd()
        self.ckdd.ck_dd_ck(self.driver2, me, filename, count)
        pyautogui.hotkey('alt', 'tab')
        time.sleep(2)
        self.skTop.sk_top_click_sx()
        self.skJk.sk_jk_scyet(self.driver)
        for i in range(count):
            dm = self.skJk.sk_jk_get_dm().split('：')[1]
            assert '5FCH3WWNJBVGVW7' == dm
        # 修改卡种
        self.skJk.sk_jk_click_xgkz()
        time.sleep(1)
        self.skJk.sk_jk_input_mz("60")
        time.sleep(1)
        self.skJk.sk_jk_click_xgtj()
        time.sleep(1)
        text = self.skJk.sk_jk_get_tcts()
        assert "你并没有此卡种信息配置的收卡汇率" in text

    # 修改卡种失败（修改后价值低于原卡价值）
    def test07_sk_jk(self):
        pyautogui.press('esc')
        # 修改卡种
        self.skJk.sk_jk_click_xgkz()
        time.sleep(1)
        self.skJk.sk_jk_input_mz("50")
        time.sleep(1)
        self.skJk.sk_jk_click_xgtj()
        time.sleep(0.5)
        text = self.skJk.sk_jk_get_xgts()
        assert "低于原卡价值" in text

    # 修改成功（修改后价值低于原卡价值）,上传图片
    def test08_sk_jk(self):
        click_paste_img("steam_card.png", self.skJk.sk_jk_get_xgtp(), self.driver)
        time.sleep(2)
        self.skJk.sk_jk_click_xgtj()
        time.sleep(0.5)
        text = self.skJk.sk_jk_get_xgcg()
        assert "修改成功" in text
        me = self.skJk.sk_jk_get_mes()[0].text
        assert "50" == me

    # 修改成功（修改值高于原价值)
    def test09_sk_jk(self):
        # 修改卡种
        self.skJk.sk_jk_click_xgkz()
        time.sleep(1)
        self.skJk.sk_jk_input_mz("100")
        time.sleep(1)
        self.skJk.sk_jk_click_xgtj()
        time.sleep(0.5)
        text = self.skJk.sk_jk_get_xgcg()
        me = self.skJk.sk_jk_get_mes()[0].text
        assert "修改成功" in text
        assert "100" == me

    # 延长时间失败
    def test10_sk_jk(self):
        for _ in range(2):
            # 点击延长时间
            self.skJk.sk_jk_click_ycsj()
            time.sleep(1)
            self.skJk.sk_jk_input_ycyy()
            time.sleep(1)
            self.skJk.sk_jk_click_yctj()
            time.sleep(1)
        # 点击延长时间
        self.skJk.sk_jk_click_ycsj()
        time.sleep(1)
        self.skJk.sk_jk_input_ycyy()
        time.sleep(1)
        self.skJk.sk_jk_click_yctj()
        time.sleep(1)
        text = self.skJk.sk_jk_get_ycts()
        assert "超过了最大可延时次数" in text
        self.skJk.sk_jk_click_ycqx()

    # 添加操作人成功
    # def test11_sk_jk(self):
    #     time.sleep(1)
    #     kpid = self.skJk.sk_jk_get_ids()[0].text
    #     # 点击添加操作人
    #     self.skJk.sk_jk_click_tjczr()
    #     time.sleep(2)
    #     self.skJk.sk_jk_click_czr()
    #     time.sleep(1)
    #     pyautogui.press('down')
    #     pyautogui.press('enter')
    #     time.sleep(1)
    #     self.skJk.sk_jk_click_tjtj()
    #     time.sleep(1)
    #     driver_new = webdriver.Chrome()
    #     driver_new.maximize_window()
    #     driver_new.get(config.URL)
    #     # 登录
    #     login_with_session('system1', driver_new)
    #     self.skJk.sk_jk_click_jk()
    #     time.sleep(2)
    #     kpid_tsteam = self.skJk.sk_jk_get_ids()[0].text
    #     assert kpid_tsteam == kpid

    # 压卡成功，处理成功
    def test12_sk_jk(self):
        time.sleep(1)
        # 点击压卡
        self.skJk.sk_jk_click_yk()
        # 输入压卡时间
        self.skJk.sk_jk_input_yksj("60")
        self.skJk.sk_jk_click_yktj()
        time.sleep(1)
        # 点击处理成功
        self.skJk.sk_jk_click_clcg()
        time.sleep(1)
        self.skJk.sk_jk_click_cgtj()
        time.sleep(2)

    # 收卡商接收卡种功能正常
    def test13_sk_jk(self):
        pyautogui.hotkey('alt', 'tab')
        time.sleep(2)
        count = 1  # 发卡数
        me = '100'  # 发卡金额
        self.ckdd.ck_dd_click_jdd()
        self.ckdd.ck_dd_ck(self.driver2, me, "sephora_card1.png", count, "t-Sephora")
        pyautogui.hotkey('alt', 'tab')
        time.sleep(2)
        self.skTop.sk_top_click_sx()
        try:
            time.sleep(2)
            # 判断是否收到卡片
            e = self.skJk.sk_jk_get_ids()
            assert len(e) == 1
            # 点击接收sephora
            self.skJk.sk_jk_click_jssephora()
            time.sleep(1)
            e = self.driver.find_elements(*page.web_sk_jk_id)
            assert len(e) == 0
            pyautogui.hotkey('alt', 'tab')
            time.sleep(2)
            self.ckdd.ck_dd_click_jdd()
            self.ckdd.ck_dd_ck(self.driver2, me, "sephora_card1.png", count, "t-Sephora")
            pyautogui.hotkey('alt', 'tab')
            self.skTop.sk_top_click_sx()
            time.sleep(2)
            e = self.driver.find_elements(*page.web_sk_jk_id)
            assert len(e) == 0
            self.skJk.sk_jk_click_jssephora()
            time.sleep(2)
            e = self.skJk.sk_jk_get_ids()
            assert len(e) == 2
            self.skJk.sk_jk_get_ckkms()[0].click()
            self.skJk.sk_jk_get_ckkms()[1].click()
            self.skJk.sk_jk_click_clcg()
            self.skJk.sk_jk_click_cgtj()
            time.sleep(2)
            self.skJk.sk_jk_click_clcg()
            self.skJk.sk_jk_click_cgtj()
        except Exception as e:
            self.log.error("接卡页未接到卡片,原因{}".format(e))
            # 截图
            self.skJk.base_get_img()
            # 抛异常
            raise
